package com.core.request.bo;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.core.request.dao.VisaRequestDAO;
import com.core.request.pojo.VisaCancelDto;
import com.core.request.pojo.VisaForDownloadReturnReportDto;
import com.core.request.pojo.VisaForImportReturnReportDto;
import com.core.request.pojo.VisaForImportReturnReportVisitorDto;
import com.core.request.pojo.VisaRequestSc;
import com.core.request.pojo.VisaResponse;
import com.util.Consts;
import com.util.DateUtil;
import com.core.request.pojo.VisaAddUpdateDtoSql;

@Service
public class VisaRequestBo {
	@Resource
	private VisaRequestDAO visaRequestDAO;

	// =========================visa==============================//
	public VisaAddUpdateDtoSql getVisaForAddUpdate() {
		VisaRequestSc visaRequestSc = new VisaRequestSc();
		// 1分钟之前新增的才能推送,以避免新增错误,修改内容
		visaRequestSc.setAddDateHigh(DateUtil.addHourMin(new Date(), 0, -1));
		VisaAddUpdateDtoSql visa = visaRequestDAO.getVisaForAddUpdate(visaRequestSc);
		return visa;
	}

	public VisaCancelDto getVisaForCancel() {
		// 1, 从visa_application_info表获取要撤消的visa
		VisaCancelDto dto = visaRequestDAO.getVisaToCancelFromVisaTable();

		// 2, 若没有，则从visa_cancel表获取要撤消的visa
		if (dto == null) {
			dto = visaRequestDAO.getVisaToCancelFromCancelTable();
		}

		return dto;
	}

	// visaCancelId非空，则更新visa_cancel表的状态
	public void updateVisaState(VisaResponse visaResponse) {
		visaResponse.setSubmitDate(new Date());

		if (visaResponse.getVisaCancelId() == null || visaResponse.getVisaCancelId() <= 0) {
			visaRequestDAO.updateVisaState(visaResponse);
		} else {
			// visaCancelId非空，则更新visa_cancel表的状态
			visaRequestDAO.updateVisaCancelState(visaResponse);
		}
	}

	// =========================return_report==============================//
	// --upload
	public VisaForImportReturnReportDto getVisaForImportReturnReport() {
		VisaForImportReturnReportDto dto = visaRequestDAO.getVisaForImportReturnReport();

		// visitors
		if (dto != null) {
			List<VisaForImportReturnReportVisitorDto> visitorList = visaRequestDAO.searchVisitorForVisa(dto.getVisaInfoId());
			dto.setVisitorList(visitorList);
		}

		return dto;
	}

	public void updateReturnReportState(VisaResponse visaResponse) {
		visaResponse.setSubmitDate(new Date());
		visaRequestDAO.updateReturnReportState(visaResponse);
	}

	// --download.
	public VisaForDownloadReturnReportDto getVisaForDownloadReturnReport() {
		VisaForDownloadReturnReportDto dto = visaRequestDAO.getVisaForDownloadReturnReport();
		return dto;
	}

	public void updateReturnReportDownloadState(VisaResponse visaResponse) {
		if (Consts.RETURN_REPORT_DOWNLOAD_STATE_SUCC == visaResponse.getReturnReportDownloadState()) {
			visaResponse.setReturnReportPdfDownloadTime(new Date());
		}
		visaRequestDAO.updateReturnReportDownloadState(visaResponse);
	}

}
